//
//  AppDelegate.swift
//  Weibo8023
//
//  Created by styshy on 15/12/6.
//  Copyright © 2015年 sz. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        
        // 1、UI当中有几个控制器默认的view是没有背景颜色的，我们在使用的时候一定要给背景颜色赋值
        // 这样有利于渲染，否则手机加载页面的时候会有卡顿的现象 (view.backgroundColor值为nil)
        // * UIWindow
        // * UIViewController
        // * UITabbarController
        // * UICollectionViewConroller
        
        // 2、而 UITableViewController 的视图view的背景颜色是白色的，不需要主动给背景赋值
        
        
        
        
        // 测试
        SQLiteManager.sharedManager
        
        QorumLogs.test()
        
        // 设置全局样式
        setupAppearance()
    
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        
        // 建议给每个UIView设置背景颜色，否则会造成卡顿现象
        window?.backgroundColor = UIColor.whiteColor()
        window?.rootViewController = defaultRootViewController
        
        window?.makeKeyAndVisible()
        
        // 设置通知
        NSNotificationCenter.defaultCenter().addObserverForName(WBSwitchRootViewControllerNotification, object: nil, queue: nil) {[weak self] (notification) -> Void in
            
            // 如果object的值为nil，则直接返回到Main视图，否则Welcome视图
           let vc =  notification.object != nil ? WelcomViewController() : MainViewController()
            
            self?.window?.rootViewController = vc
        }
        
        
        return true
    }
    
/// 注销通知
    deinit{
        // 注销指定的通知
        NSNotificationCenter.defaultCenter().removeObserver(self,// 监听者
            name: WBSwitchRootViewControllerNotification,// 监视的通知
            object: nil)// 发送通知的对象
    }

    func setupAppearance(){
        UITabBar.appearance().tintColor = HMAppearanceTintColor
        UINavigationBar.appearance().tintColor = HMAppearanceTintColor
    }
    
    
    // 定期清除app缓存
    func applicationDidEnterBackground(application: UIApplication) {
        StatusDAL.clearDataCache()
    }
    
}


// MARK:- 界面切换代码
extension AppDelegate{
    /// 计算型属性
    private var defaultRootViewController:UIViewController{
        if UserAccountViewModel.sharedUserAccount.userLogin{
            return isNewVersion ? NewFeatureViewController() : WelcomViewController()
        }
        
        return MainViewController()
    }
    
/// 判断是否是新版本
    private var isNewVersion:Bool {
        
        let currentVersion = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        let version = Double(currentVersion)!
        
        let sandboxVersionKey = "sandboxVersionKey"
        let sandboxVersion = NSUserDefaults.standardUserDefaults().doubleForKey(sandboxVersionKey)
        
        NSUserDefaults.standardUserDefaults().setDouble(version, forKey: sandboxVersionKey)
        return version > sandboxVersion
    }
}
